package com.app.qbake.module.mine;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;

import com.aigestudio.wheelpicker.utils.WheelPickerFactory;
import com.aigestudio.wheelpicker.widget.IWheelVo;
import com.wind.base.mvp.view.DaggerMvpFragment;
import com.wind.base.request.BaseRequest;
import com.wind.base.utils.ActivityUtil;
import com.wind.coder.annotations.Api;
import com.wind.coder.annotations.Heros;
import com.wind.coder.annotations.Param;
import com.wind.data.mine.ShipmentRequest;
import com.wind.data.mine.ShipmentResponse;
import com.app.qbake.R;
import com.app.qbake.module.mine.di.ShipmentComponent;
import com.app.qbake.module.mine.event.ShipmentEvent;
import com.app.qbake.module.mine.layout.ShipmentPhotoLayout;
import com.app.qbake.module.mine.mvp.ShipmentView;
import com.app.qbake.module.mine.presenter.ShipmentPresenter;
import com.wind.toastlib.ToastUtil;
import com.wind.view.ValidateEditText;
import com.wind.view.ValidateTextView;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

@Heros(
        param = @Param(
                viewCanonicalName = "com.app.qbake.module.mine.mvp.ShipmentView",
                responseCanonicalName = "com.wind.data.mine.ShipmentResponse",
                requestCanonicalName = "com.wind.data.mine.ShipmentRequest"

        ),
        api = @Api(httpMethod = Api.HttpMethod.POST, url = "order/express")
)
public class ShipmentFragment extends DaggerMvpFragment<ShipmentView,ShipmentPresenter,ShipmentComponent>
implements ShipmentView, ShipmentPhotoLayout.UploadFileListener {

        private static final String ARGS_KEY_ORDERID = "args_key_orderid";
        @BindView(R.id.layout_express)
        ValidateTextView layout_express;
        @BindView(R.id.et_express)
        ValidateEditText et_express;
        @Inject
        ShipmentPresenter presenter;
        @BindView(R.id.iv_1)
        ImageView iv_1;
        @BindView(R.id.iv_2)
        ImageView iv_2;

        ShipmentPhotoLayout mShipmentPhotoLayout;
        private String mOrderId;
        @Override
        protected int getLayoutRes() {
                return R.layout.fragment_shipment;
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
                super.onViewCreated(view, savedInstanceState);
                mOrderId=getArguments().getString(ARGS_KEY_ORDERID);
                mShipmentPhotoLayout=new ShipmentPhotoLayout(getActivity(),iv_1,iv_2);
                mShipmentPhotoLayout.setUploadFileListener(this);
        }

        @Override
        public ShipmentPresenter createPresenter() {
                return presenter;
        }

        @Override
        public void onSuccess(ShipmentResponse response) {
                ToastUtil.showToast(getActivity(),"成功发货");
                EventBus.getDefault().post(new ShipmentEvent());
                ActivityUtil.finish(getActivity());
        }


        @OnClick({R.id.layout_express,R.id.tv_commit})
        public void onViewClick(View view){
                switch (view.getId()){

                        case R.id.tv_commit:
                                if (validate()){
                                    showOpLoadingIndicator();
                                    mShipmentPhotoLayout.uploadFile();

                                }
                                break;
                        case R.id.layout_express:
                                WheelPickerFactory.showWheelAPicker(layout_express, new WheelPickerFactory.OnWheelClickListener() {
                                        @Override
                                        public void onResult(View v, IWheelVo[] result, int[] indexs, String[] unit) {
                                                layout_express.setText(result[0].getLabel());
                                        }
                                },R.xml.wheel_express,"");
                                break;
                }
        }



        private BaseRequest buildRequest(List<String> urls) {
                ShipmentRequest request=new ShipmentRequest();
                request.setOrder_id(mOrderId);
                request.setExpress_name(getText(layout_express));
                request.setOrder_no(getText(et_express));
                request.setOrder_url(urls.get(0));
                request.setBaby_url(urls.get(1));
                return request;
        }

        private boolean validate() {

                return layout_express.validate("请选择快递公司")
                        && et_express.validate("请输入快递单号")
                        && mShipmentPhotoLayout.validate("请上传订单和商品照片");

        }


        public static Fragment getInstance(String orderId){

                ShipmentFragment f=new ShipmentFragment();
                Bundle args=new Bundle();
                args.putString(ARGS_KEY_ORDERID,orderId);
                f.setArguments(args);
                return f;

        }

        @Override
        public void onUploadError() {
                showError("上传失败请重试");
        }

        @Override
        public void onUploadFileProgress() {

        }

        @Override
        public void onUploadFileSuccess(List<String> urls) {
                presenter.execute(buildRequest(urls));
        }
}
